﻿# --------------------------------------------------- #
# Azure Active Directory Connect (AADC) Configuration #
# --------------------------------------------------- #
param([string]$virtualMachineService, [string]$virtualMachineName)

Write-Information "Virtual machine '$virtualMachineName' executing Azure Active Directory Connect '$($script.type)' configuration ... " -NoNewLine $true
$session = Get-VirtualMachineInstancePowerShellSession -Subscription $subscription -VirtualMachineName $virtualMachineName -AllowNetworkConnections
switch ($script.type)
{
	"Install"
	{
		Invoke-Command -Session $session -ScriptBlock {
            # Install Azure Active Directory Connect
            if ((Start-Process -FilePath "S:\AzureADConnect.msi" -ArgumentList "/Quiet /NoRestart /Log AzureADConnect.log" -PassThru -Wait).ExitCode -ne 0)
            {
                throw "Azure Active Directory Connect installation failed. Check '$Env:USERPROFILE\Documents\AzureADConnect.log' for more details."
            }
            # Install Microsoft Online Services Sign-in Assistant
            if ((Start-Process -FilePath "C:\Program Files\Microsoft Azure Active Directory Connect\SetupFiles\msoidcli_64.msi" -ArgumentList "/Quiet /NoRestart /Log msoidcli_64.log" -PassThru -Wait).ExitCode -ne 0)
            {
                throw "Microsoft Online Services Sign-in Assistant installation failed. Check '$Env:USERPROFILE\Documents\msoidcli_64.log' for more details."
            }
            # Install Windows Azure Active Directory Module for Windows PowerShell
            if ((Start-Process -FilePath "C:\Program Files\Microsoft Azure Active Directory Connect\SetupFiles\AdministrationConfig-en.msi" -ArgumentList "/Quiet /NoRestart /Log AdministrationConfig-en.log" -PassThru -Wait).ExitCode -ne 0)
            {
                throw "Windows Azure Active Directory Module for Windows PowerShell installation failed. Check '$Env:USERPROFILE\Documents\AdministrationConfig-en.log' for more details."
            }
            # Install Microsoft Visual C++ 2013 Redistributable Package
            if ((Start-Process -FilePath "C:\Program Files\Microsoft Azure Active Directory Connect\SetupFiles\vcredist120\vcredist_x64.exe" -ArgumentList "/Install /Quiet /NoRestart /Log vcredist_x64-en.log" -PassThru -Wait).ExitCode -ne 0)
            {
                throw "Microsoft Visual C++ 2013 Redistributable Package installation failed. Check '$Env:USERPROFILE\Documents\vcredist_x64.log' for more details."
            }
            # Install Microsoft SQL Server 2012 Native Client
            if ((Start-Process -FilePath "C:\Program Files\Microsoft Azure Active Directory Connect\SetupFiles\SqlNCli.msi" -ArgumentList "/Quiet /NoRestart IAcceptSqlNCliLicenseTerms=Yes /Log SqlNCli.log" -PassThru -Wait).ExitCode -ne 0)
            {
                throw "Microsoft SQL Server 2012 Native Client installation failed. Check '$Env:USERPROFILE\Documents\SqlNCli.log' for more details."
            }
            # Install Microsoft SQL Server 2012 Command Line Utilities
            if ((Start-Process -FilePath "C:\Program Files\Microsoft Azure Active Directory Connect\SetupFiles\SqlCmdLnUtils.msi" -ArgumentList "/Quiet /NoRestart /Log SqlCmdLnUtils.log" -PassThru -Wait).ExitCode -ne 0)
            {
                throw "Microsoft SQL Server 2012 Command Line Utilities installation failed. Check '$Env:USERPROFILE\Documents\SqlCmdLnUtils.log' for more details."
            }
		}
    }
	"Configure"
	{
		Invoke-Command -Session $session -ScriptBlock {
            param([string]$userName, [string]$password, [string]$sqlServer, [string]$sqlDatabase, [string]$admins, [string]$operators, [string]$browse, [string]$passwordSet)
            # Install Synchronization Service
            $arguments = "/Quiet /NoRestart /Log Synchronization_Service.log"
            $arguments += " STORESERVER=`"$sqlServer`" SQLDB=`"$sqlDatabase`""
            $arguments += " SERVICEDOMAIN=$($userName.Split("\")[0]) SERVICEACCOUNT=$($userName.Split("\")[1]) SERVICEPASSWORD=$password"
            $arguments += " GROUPADMINS=$admins GROUPOPERATORS=$operators GROUPBROWSE=$browse GROUPPASSWORDSET=$passwordSet"
            if ((Start-Process -FilePath "C:\Program Files\Microsoft Azure Active Directory Connect\SetupFiles\Synchronization Service.msi" -ArgumentList $arguments -PassThru -Wait).ExitCode -ne 0)
            {
                throw "Synchronization Service installation failed. Check '$Env:USERPROFILE\Documents\Synchronization_Service.log' for more details."
            }
            # Install Forefront Identity Manager Windows Azure Active Directory Connector
            if ((Start-Process -FilePath "C:\Program Files\Microsoft Azure Active Directory Connect\SetupFiles\AADConnector.msi" -ArgumentList "/Quiet /NoRestart ALLUSERS=1 FIMSYNCSERVICE=`"C:\Program Files\Microsoft Azure AD Sync`" /Log AADConnector.log" -PassThru -Wait).ExitCode -ne 0)
            {
                throw "Forefront Identity Manager Windows Azure Active Directory Connector installation failed. Check '$Env:USERPROFILE\Documents\AADConnector.log' for more details."
            }
		} -ArgumentList $script.userName, $script.password, $script.sqlServer, $script.sqlDatabase, $script.admins, $script.operators, $script.browse, $script.passwordSet
    }
	default
	{
		throw "Azure Active Directory Connect type '$($script.type)' is not supported."
	}
}
Remove-PSSession $session
Write-Text "done."